package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.Role;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.service.impl.RoleServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-17 23:12
 */
@RestController
@RequestMapping("/role")
@ResponseBody
public class RoleController {
    @Autowired
    private RoleServiceImpl roleService;

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @PostMapping("/create")
    public RespUtils<Boolean> create(HttpSession session, @RequestBody Role role) {
        try {
            boolean isCreated = roleService.insertRole(session, role);
            if (isCreated) {
                return RespUtils.success(isCreated);
            }
            return RespUtils.fail("添加角色失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/list")
    public RespUtils<List<Role>> list() {
        try {
            return RespUtils.success(roleService.list());
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
